//
//  LCTravelListViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/6/6.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTravelListViewController.h"
#import "LCTravelCollectionViewCell.h"
#import "LCTravelDetailViewController.h"

#import "LCGlobalService.h"

#import "LCTravelCollectionHeadView.h"


@interface LCTravelListViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *navBgView;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (assign, nonatomic)NSInteger page;
@property (strong, nonatomic)NSMutableArray * dataArr;

@end

@implementation LCTravelListViewController

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self setContentViewRefresh];
    [self getTravelData:1];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)setContentViewRefresh {
    self.navBgView.backgroundColor = [UIColor gradientChange_ColorWithSize:CGSizeMake(ScreenW, NavgationHeight) direction:GradientChangeColorDirectionLevel startColor:[UIColor colorWithHexString:@"#00AAFE"] endColor:[UIColor colorWithHexString:@"#7616E3"]];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCTravelCollectionHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LCTravelCollectionHeadView"];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getTravelData:1];
    }];
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getTravelData:self.page];
    }];
    self.collectionView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

-(void)getTravelData:(NSInteger)page {
    [LCGlobalService getTravelListWithPage:page Block:^(BOOL isSuccess, NSArray<LCTravelModel *> *list, NSInteger total) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.collectionView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        LCTravelCollectionHeadView * view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LCTravelCollectionHeadView" forIndexPath:indexPath];
        return view;
    } else {
        return nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(ScreenW, 139*proW + 94);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(7, 17, 14, 17);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cellW = ceilf((ScreenW-48)/2.0);
//    CGFloat cellW = (ScreenW-48)/2;
    CGFloat cellH = cellW+95;
    return CGSizeMake(cellW, cellH);
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCTravelCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCTravelCollectionViewCell" forIndexPath:indexPath];
    cell.travel = self.dataArr[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCTravelDetailViewController * detailVC = [LCTravelDetailViewController xx_instantiateFromStoryboardNamed:@"Global"];
    detailVC.travel = self.dataArr[indexPath.item];
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
